<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<script src="lib/react.development.js"></script>
	<script src="lib/react-dom.development.js"></script>
	<script src="lib/babel.min.js"></script>
	<style>
		#root button{
			padding:5px;
			margin:5px;
		}
		#root button.active{
			background: red;
		}
		#root div{
			width: 500px;
			height: 300px;
			border:1px solid green;
		}
	</style>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel">
	const root = ReactDOM.createRoot(document.querySelector("#root"));
	// 1- 数据大多来自于接口。
	// 2- 根据自身的需求决定是否要使用state
	
	
	class App extends React.Component{
		state = {
			activeIndex:0,
			bookArr:[]
		}
		render(){
			return (
				<>
					{
						this.state.bookArr.map((item,index)=>(
							<button onClick={()=>this.setState({activeIndex:index})} className={this.state.activeIndex===index?"active":""} key={index}>{item.type}</button>
						))
					}
					{
						this.state.bookArr.map((item,index)=>(
							<div style={{display:this.state.activeIndex===index?"block":"none"}} key={index}>
								{
									item.bookList.map(info=>(
										<p key={info.id}>{info.bookName}</p>
									))
								}
							</div>
						))
					}
				</>
			)
		}
		componentDidMount(){
			// 当组件挂载完毕之后执行
			const xhr = new XMLHttpRequest();
			xhr.responseType = "json";
			xhr.open("get","./bookArr.json");
			xhr.send();
			// xhr.onload = function(){
			// 	this.setState({
			// 		bookArr:xhr.response
			// 	})
			// }.bind(this);
			
			xhr.onload = ()=>{
				this.setState({
					bookArr:xhr.response
				})
			}
			
		}
	}
	root.render(<App/>)
</script>
</html>